﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UpdateLib.Model;
using System.Xml;
using System.Net;
using System.IO;

namespace UpdateLib.Utils {
    public class Utils {
        /// <summary>
        /// Check for updates
        /// </summary>
        /// <returns></returns>
        public static UpdateInfo CheckForUpdates(string updateUrl, string proxy ="", string domain="", string user="", string password="") {

            UpdateInfo updateInfo = new UpdateInfo();
            WebProxy wp = null;
            WebClient wc = null;
            XmlTextReader reader = null;

            try {            
            if (!string.IsNullOrWhiteSpace(proxy)) {
                wp = new WebProxy(proxy);
                if (!string.IsNullOrWhiteSpace(domain))
                    wp.Credentials = new NetworkCredential(user, password, domain);
                else
                    wp.Credentials = new NetworkCredential(user, password);

                wc = new WebClient();
                wc.Proxy = wp;

                MemoryStream ms = new MemoryStream(wc.DownloadData(updateUrl));
                reader = new XmlTextReader(ms);
            } else {
                reader = new XmlTextReader(updateUrl);                
            }                   
                // provide the XmlTextReader with the URL of
                // our xml document                                
                // simply (and easily) skip the junk at the beginning
                reader.MoveToContent();
                // internal - as the XmlTextReader moves only
                // forward, we save current xml element name
                // in elementName variable. When we parse a
                // text node, we refer to elementName to check
                // what was the node name
                string elementName = "";
                // we check if the xml starts with a proper
                // "ourfancyapp" element node
                if ((reader.NodeType == XmlNodeType.Element) &&
                    (reader.Name == "versionparam")) {
                    while (reader.Read()) {
                        // when we find an element node,
                        // we remember its name
                        if (reader.NodeType == XmlNodeType.Element)
                            elementName = reader.Name;
                        else {
                            // for text nodes...
                            if ((reader.NodeType == XmlNodeType.Text) &&
                                (reader.HasValue)) {
                                // we check what the name of the node was
                                switch (elementName) {
                                    case "appversion":
                                        // thats why we keep the version info
                                        // in xxx.xxx.xxx.xxx format
                                        // the Version class does the
                                        // parsing for us
                                        updateInfo.AppVersion = new Version(reader.Value);
                                        break;
                                    case "libversion":
                                        updateInfo.LibVersion = new Version(reader.Value);
                                        break;
                                    case "helpversion":
                                        updateInfo.HelpVersion = new Version(reader.Value);
                                        break;
                                    case "confversion":
                                        updateInfo.ConfVersion = new Version(reader.Value);
                                        break;
                                    case "liburl":
                                        updateInfo.LibUpdateUrl = reader.Value;
                                        break;
                                    case "helpurl":
                                        updateInfo.HelpUpdateUrl = reader.Value;
                                        break;
                                    case "confurl":
                                        updateInfo.ConfUpdateUrl = reader.Value;
                                        break;
                                    case "appurl":
                                        updateInfo.AppUpdateUrl = reader.Value;
                                        break;
                                }
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                return null;
            } finally {
                if (reader != null) reader.Close();
            }

            return updateInfo;
        }
    }
}
